package com.qiji.video.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiji.video.bean.Admins;
import com.qiji.video.exception.ServiceException;
import com.qiji.video.mapper.AdminsMapper;
import com.qiji.video.mapper.extend.AdminsExtendMapper;
import com.qiji.video.service.IAdminService;
import com.qiji.video.util.JwtUtil;
import com.qiji.video.util.Md5Util;
import com.qiji.video.util.ResultCode;
import org.mapstruct.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private AdminsMapper adminsMapper;
    @Autowired
    private AdminsExtendMapper extendMapper;
    @Override
    public Admins adminLogin(String username,String password) throws ServiceException {
        HashMap map=new HashMap();
        Admins admin = null;
        if (username.indexOf("@")>=0){
            map.put("email",username);
             admin = extendMapper.adminLoginByEmail(username);
        }else {
            map.put("phone",username);
            admin = extendMapper.adminLoginByPhone(username);
        }
        if (admin == null){
            throw new ServiceException(ResultCode.ADMIN_NULL);
        }else{
            if (!Md5Util.getMd5Key(password).equals(admin.getPassword())){
                throw new ServiceException(ResultCode.PASSWORD_ERROR);
            }
        }
        map.put("loginTime",new Timestamp(System.currentTimeMillis()));
        extendMapper.updateLoginTime(map);
        return admin;
    }
    @Override
    public List<Admins> selectAllAdmins() throws ServiceException{

        return  extendMapper.selectAllAdmins();



    }

    @Override
    public PageInfo<Admins> selectAllAdmin(int pageNum, int pageSize, String name) throws ServiceException {

        PageHelper.startPage(pageNum,pageSize);
        List<Admins> admins = extendMapper.selectListByName(name);

        return new PageInfo<>(admins);
    }

    @Override
    public void updateByPrimaryKey(Admins admins) throws ServiceException {
        adminsMapper.updateByPrimaryKey(admins);
    }

    @Override
    public void insert(Admins admins) throws ServiceException {
        adminsMapper.insert(admins);
    }

    @Override
    public void deleteByPrimaryKey(Integer id) throws ServiceException {
        adminsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateByPrimaryKeySelective(Integer id,String password) throws ServiceException {
        Admins admins = new Admins();
        admins.setId(id);
        admins.setPassword(password);
        adminsMapper.updateByPrimaryKeySelective(admins);
    }
    @Override   //根据请求头id查询个人详细信息
    public Admins currentUserInfo(Integer userId){
        return  adminsMapper.selectByPrimaryKey(userId);
    }
    @Override   //根据请求头id修改个人信息
    public void updateAdminInfoByToken(Integer userId,Admins admins){

        admins.setId(userId);
        adminsMapper.updateByPrimaryKeySelective(admins);

    }
    @Override   //根据请求头id修改个人密码
    public void updateAdminPassword(Integer userId,String password) throws ServiceException {

        Admins admins = new Admins();
        admins.setId(userId);
        admins.setPassword(password);
        adminsMapper.updateByPrimaryKeySelective(admins);
    }

}
